﻿using System.Drawing.Drawing2D;

namespace Tetris.Game.ControlsBase.Common
{
    public static class GraphicsEx
    {
        private static Graphics TempGraphics;

        /// <summary>
        /// 提供一个Graphics，常用于需要计算文字大小时
        /// </summary>
        /// <returns>大小</returns>
        public static Graphics Graphics()
        {
            if (TempGraphics == null)
            {
                Bitmap bmp = new Bitmap(1, 1);
                TempGraphics = bmp.Graphics();
            }

            return TempGraphics;
        }
        /// <summary>
        /// 图片的绘图图元
        /// </summary>
        /// <param name="image">图片</param>
        /// <returns>绘图图元</returns>
        public static Graphics Graphics(this Image image)
        {
            return System.Drawing.Graphics.FromImage(image);
        }
        /// <summary>
        /// 计算文字大小
        /// </summary>
        /// <param name="text">文字</param>
        /// <param name="font">字体</param>
        /// <returns>大小</returns>
        public static SizeF MeasureString(this string text, Font font)
        {
            return Graphics().MeasureString(text, font);
        }
        public static void FillRoundedRectangle(this Graphics g, Brush brush, int x, int y, int width, int height, int borderRadius)
        {
            int diameter = borderRadius * 2;
            Size size = new Size(diameter, diameter);
            Rectangle arcRect = new Rectangle(new Point(x, y), size);

            g.FillRectangle(brush, x + borderRadius, y, width - diameter, borderRadius);
            g.FillRectangle(brush, x, y + borderRadius, width, height - diameter);
            g.FillEllipse(brush, arcRect);
            arcRect.X = x + width - diameter;
            g.FillEllipse(brush, arcRect);
            arcRect.Y = y + height - diameter;
            g.FillEllipse(brush, arcRect);
            arcRect.X = x;
            g.FillEllipse(brush, arcRect);
            g.FillRectangle(brush, x + borderRadius, y + height - borderRadius, width - diameter, borderRadius);
        }
        public static void DrawRoundedRectangle(this Graphics g, Pen pen, Rectangle rect, int borderRadius)
        {
            int diameter = borderRadius * 2;
            Size size = new Size(diameter, diameter);
            Rectangle arcRect = new Rectangle(rect.Location, size);

            g.DrawArc(pen, arcRect, 180, 90);
            arcRect.X = rect.Right - diameter;
            g.DrawArc(pen, arcRect, 270, 90);
            arcRect.Y = rect.Bottom - diameter;
            g.DrawArc(pen, arcRect, 0, 90);
            arcRect.X = rect.Left;
            g.DrawArc(pen, arcRect, 90, 90);

            g.DrawLine(pen, rect.Left + borderRadius, rect.Top, rect.Right - borderRadius, rect.Top);
            g.DrawLine(pen, rect.Left + borderRadius, rect.Bottom, rect.Right - borderRadius, rect.Bottom);
            g.DrawLine(pen, rect.Left, rect.Top + borderRadius, rect.Left, rect.Bottom - borderRadius);
            g.DrawLine(pen, rect.Right, rect.Top + borderRadius, rect.Right, rect.Bottom - borderRadius);
        }

        public static Color StepColor(this Color color, int alpha)
        {
            if (alpha == 100)
            {
                return color;
            }

            byte a = color.A;
            byte r = color.R;
            byte g = color.G;
            byte b = color.B;
            double num = ((double)Math.Max(alpha, 0) - 100.0) / 100.0;
            float num2;
            if (num > 100.0)
            {
                num2 = 255f;
                num = 1.0 - num;
            }
            else
            {
                num2 = 0f;
                num = 1.0 + num;
            }

            r = (byte)BlendColor((int)r, num2, num);
            g = (byte)BlendColor((int)g, num2, num);
            b = (byte)BlendColor((int)b, num2, num);
            return Color.FromArgb(a, r, g, b);
        }

        private static double BlendColor(double fg, double bg, double alpha)
        {
            double num = bg + alpha * (fg - bg);
            if (num < 0.0)
            {
                num = 0.0;
            }

            if (num > 255.0)
            {
                num = 255.0;
            }

            return num;
        }
        public static Color SetAlpha(this Color color, int alpha)
        {
            return Color.FromArgb(alpha, color.R, color.G, color.B);
        }
   

        /// <summary>
        /// 绘图路径
        /// </summary>
        /// <param name="rect">区域</param>
        /// <returns>路径</returns>
        public static GraphicsPath GraphicsPath(this Rectangle rect)
        {
            Point[] points = new Point[] {
                    new Point(rect.Left, rect.Top),
                    new Point(rect.Right, rect.Top),
                    new Point(rect.Right, rect.Bottom),
                    new Point(rect.Left, rect.Bottom),
                    new Point(rect.Left, rect.Top) };
            return points.Path();
        }
        /// <summary>
        /// 创建路径
        /// </summary>
        /// <param name="points">点集合</param>
        /// <returns>路径</returns>
        public static GraphicsPath Path(this Point[] points)
        {
            GraphicsPath path = new GraphicsPath();
            path.Reset();
            path.AddPolygon(points);
            return path;
        }
    }
}
